include('shared.lua')

// Materials

local Materials = {}

// Set material

function ENT:Material(Texture)
	if (Materials[Texture]) then
		return Materials[Texture]
	else
		Materials[Texture] = Material(Texture)
		
		return Materials[Texture]
	end
end

// Draw

function ENT:Draw()
	if (LocalPlayer():GetEyeTrace().Entity == self.Entity && EyePos():Distance(self.Entity:GetPos()) < 512) then
		self:DrawEntityOutline(1.0)
		
		if (self:GetOverlayText() != "") then
			AddWorldTip(self.Entity:EntIndex(), self:GetOverlayText(), 0.5, self.Entity:GetPos(), self.Entity)
		end
	end
	
	// Draw model
	
	self.Entity:DrawModel()
	
	// Variables
	
	local Position = self.Entity:GetPos()
	local Width = self.Entity:GetNetworkedInt("Width")
	local Height = self.Entity:GetNetworkedInt("Height")
	local Texture = self.Entity:GetMaterial()
	
	// Wireframe
	
	if (Texture == "") then Texture = "models/wireframe" end
	
	// Material
	
	render.SetMaterial(self:Material(Texture))
	
	// Colour
	
	local Colour = Color(255, 255, 255, 255)

	// Quad
	
	self:DrawAxisPlaneX(Position, Width, Height, Colour)
	self:DrawAxisPlaneY(Position, Width, Height, Colour)
	self:DrawAxisPlaneZ(Position, Width, Height, Colour)
	self:DrawAxisPlaneX(Position + Vector(Width, Width, 0), -Width, Height, Colour)
	self:DrawAxisPlaneY(Position + Vector(Width, Width, 0), -Width, Height, Colour)
	self:DrawAxisPlaneZ(Position + Vector(Width, Width, Height), -Width, Height, Colour)
end 

// Draw axis plane X

function ENT:DrawAxisPlaneX(Position, Width, Height, Colour)
	render.DrawQuadEasy(Position + Vector(0, Width / 2, Height / 2), Vector(-1, 0, 0), Width, Height, Colour)
	render.DrawQuadEasy(Position + Vector(0, Width / 2, Height / 2), Vector(1, 0, 0), Width, Height, Colour)
end

// Draw axis planeY

function ENT:DrawAxisPlaneY(Position, Width, Height, Colour)
	render.DrawQuadEasy(Position + Vector(Width / 2, 0, Height / 2), Vector(0, -1, 0), Width, Height, Colour)
	render.DrawQuadEasy(Position + Vector(Width / 2, 0, Height / 2), Vector(0, 1, 0), Width, Height, Colour)
end

// Draw axis planeY

function ENT:DrawAxisPlaneZ(Position, Width, Height, Colour)
	render.DrawQuadEasy(Position + Vector(Width / 2, Width / 2, 0), Vector(0, 0, -1), Width, Width, Colour)
	render.DrawQuadEasy(Position + Vector(Width / 2, Width / 2, 0), Vector(0, 0, 1), Width, Width, Colour)
end

// Think

function ENT:Think()
	// Width and height
	
	local Width = self.Entity:GetNetworkedInt("Width")
	local Height = self.Entity:GetNetworkedInt("Height")
	
	// Render bounds
	
	local Minimum = Vector(-Width, -Width, -Height)
	local Maximum = Vector(Width, Width, Height)
	
	self.Entity:SetRenderBounds(Minimum, Maximum)
end